added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSDynamicallyBuildLambdaExpressionWithField / MainForm.cs
blob38aba847b92d89f1ed6e6e47fddd76559c9fe7f9
1 /********************************** Module Header **********************************\
2 * Module Name: MainForm.cs
3 * Project: CSDynamicallyBuildLambdaExpressionWithField
4 * Copyright (c) Microsoft Corporation.
6 * This sample demonstrates how to dynamically build lambda expression and show data
7 * into DataGridView Control.
8 *
9 * This sample shows up multiple conditions jointing together and dynamically
10 * generate LINQ TO SQL. LINQ is a great way to declaratively filter and query data
11 * in a Type_Safe,Intuitive,and very expressive way.this sample achieve it. For example,
12 * the search feature in this application allow the customer to find all records that
13 * meet criteria defined on multiple columns.
15 * This source is subject to the Microsoft Public License.
16 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
17 * All other rights reserved.
19 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
22 \***********************************************************************************/
24 using System;
25 using System.Linq;
26 using DynamicCondition;
29 namespace CSDynamicallyBuildLambdaExpressionWithField
31 public partial class MainForm
33 internal MainForm()
35 InitializeComponent();
37 private NorthwindDataContext db = new NorthwindDataContext();
39 /// <summary>
40 /// Load the list of fields into the control when Winform loads.
41 /// </summary>
42 private void MainForm_Load(object sender, EventArgs e)
44 // Load the list of fields into the control
45 ConditionBuilder1.SetDataSource(db.Orders);
48 /// <summary>
49 /// Dynamically generate LINQ query and put it into DataGridView Control
50 /// </summary>
51 private void btnSearch_Click(object sender, EventArgs e)
53 // Get the Condition out of the control
54 var c = ConditionBuilder1.GetCondition<Order>();
56 // Filter out all Orders that don't match the Condition
57 // Note that the query does not actually get executed yet to due to deferred execution
58 var filteredQuery = db.Orders.Where(c);
60 // We can now perform any other operations (such as Order By or Select) on filteredQuery
61 var query = from row in filteredQuery
62 orderby row.OrderDate, row.OrderID
63 select row;
65 // Executes the query and displays the results in DataGridView1
66 dgResult.DataSource = query;
69 /// <summary>
70 /// Property DefaultInstance
71 /// </summary>
72 private static MainForm _defaultInstance;
73 public static MainForm DefaultInstance
75 get
77 if (_defaultInstance == null)
78 _defaultInstance = new MainForm();
79 return _defaultInstance;